package com.htj.erp.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Color;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.htj.erp.vo.LocationVo;
import com.kedacom.utils.StringUtil;

public class OrderProcess {
    
    private String fileName;
    
    private XSSFWorkbook  rwb = null;
    
    
    public OrderProcess(String fileName) {
        super();
        this.fileName = fileName;
        try {
            init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    public void init() throws Exception
    {
        InputStream stream = new FileInputStream(new File(fileName));
        rwb = new XSSFWorkbook(stream);
    }




    public List<LocationVo> readLocation()
    {
        XSSFSheet sheet = rwb.getSheetAt(0);
        Integer num = sheet.getLastRowNum();
        
        XSSFRow  headerRow = sheet.getRow(0);
        short cellNumber = headerRow.getLastCellNum();
        short locNameIdx = -1;
        short locAddIdx = -1;
        for(short i =0; i< cellNumber; i++)
        {
            if("支局/部门".equals(headerRow.getCell(i).getStringCellValue()))
            {
                locNameIdx = i;
            }
            if("配送地址".equals(headerRow.getCell(i).getStringCellValue()))
            {
                locAddIdx = i;
            }
        }
        ArrayList<LocationVo> list = new ArrayList<LocationVo>();
        for(short i =1; i<= num; i++)
        {
            XSSFRow  crow = sheet.getRow(i);
            XSSFCell  senderCell = crow.getCell(0);
            if(!"1108送".equals(senderCell.getStringCellValue()))
            {
            	continue;
            }
            XSSFCell  nameCell = crow.getCell(locNameIdx);
            XSSFColor  fontColor = nameCell.getCellStyle().getFont().getXSSFColor() ;
//            if(fontColor.getARGBHex().equals("FF000000"))//黑色字体不处理
//            {
//            	continue;
//            }
            
            if(StringUtil.isNotEmpty(crow.getCell(locAddIdx).getStringCellValue())){
                LocationVo lv = new LocationVo();
                lv.setAddress(crow.getCell(locAddIdx).getStringCellValue());
                lv.setName(crow.getCell(locNameIdx).getStringCellValue());
                lv.setRegion("苏州");
                if(!list.contains(lv))
                {
                    list.add(lv);
                }
            }
        }
        return list;
    }
    
    
    public static void main(String[] args)
    {
        OrderProcess op = new OrderProcess("D:\\HTJ\\订单\\订单详情20161101.xlsx");
        List<LocationVo> list = op.readLocation();
        for(LocationVo lv : list)
        {
            System.out.println(lv.getName());
        }
        LocationVo start = new LocationVo();
        start.setRegion("苏州");
        start.setAddress("曙光苑");
        start.setName("曙光苑");
        PathVo v;
        try {
            v = BaiduMapUtil.routePlanning(start,list,start);
            for(LocationVo lv : v.loc)
            {
                System.out.println(lv.getName());
            }
            System.out.println("总里程：" + v.distance);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
        
        
        
    }
    
}
